﻿using System;
using System.Drawing;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.IO;
using iTextSharp.text;
using System.Web.UI.HtmlControls;
using iTextSharp.text.html;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;

public partial class Main_UOM_uom : System.Web.UI.Page
{
    Datautility du = new Datautility();
    string username;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (Session["username"] != null)
        {
            username = Session["username"].ToString();
        }
        else
        {
            Response.Redirect("~/Login.aspx");
        }
        if (!IsPostBack)
        {
            if (Request.QueryString["View"] != null)
            {
                if (Request.QueryString["View"].ToString() == "True")
                {
                    divprint.Visible = true;
                    pnlmaindata.Visible = false;
                    getdata();
                }
            }
            else
            {
                divprint.Visible = false;
                pnlmaindata.Visible = true;
                fillgrid();
            }
        }
    }
    public void fillgrid()
    {
        DataTable dtuom = du.getdata("select u1.uomid,u1.uomsymbol,u1.convfactor,u1.qtydecimalplaces,u1.uomname as uomname,u1.primary_uomid,u2.uomname as primaryuom from ivr_uom u1 left outer join ivr_uom u2 on u1.primary_uomid=u2.uomid");
        drpprimary.DataSource = dtuom;
        drpprimary.DataTextField = "uomname";
        drpprimary.DataValueField = "uomid";
        drpprimary.DataBind();
    }
    protected void getdata()
    {
        DataTable dtuom = du.getdata("select uomid, uomname,uomsymbol from ivr_uom");
        griduom.DataSource = dtuom;
        griduom.DataBind();        
    }    
    protected void add_button_Click(object sender, EventArgs e)
    {
       DataTable dtret = du.getdata("select uomname,uomsymbol from ivr_uom where uomname='"+txtuname.Text+"'or uomsymbol='"+txtsym.Text+"'");
        if (dtret.Rows.Count > 0)
        {
          //  lblMsg.BackColor = Color.Red;
           // lblMsg.ForeColor = Color.White;
            lblMsg.Text = "Unit Name and Short Name Already Exists";
        }
        else
        {
            

            if (drpprimary.SelectedValue == "")
            {
                SqlParameter[] parm1 ={new SqlParameter("@uomname",txtuname.Text),
                               new SqlParameter("@uomsymbol",txtsym.Text),
                               new SqlParameter("@primary_uomid",drpprimary.SelectedValue),
                               new SqlParameter("@convfactor",txtconv.Text),
                               new SqlParameter("@qtydecimalplaces",txtdecimal.Text),
                               new SqlParameter("@CName",username),
                              };
                du.executeprocedure("usp_ins_uom1", parm1);
                fillgrid();
                lblMsg.Text = "Unit Added...";
                txtuname.Text = "";
                txtsym.Text = "";
                drpprimary.SelectedItem.Text = "";
                txtconv.Text = "";
                txtdecimal.Text = "";
                txtuname.Focus();
            }
            else
            {

                SqlParameter[] parm2 ={new SqlParameter("@uomname",txtuname.Text),
                               new SqlParameter("@uomsymbol",txtsym.Text),
                               new SqlParameter("@primary_uomid",drpprimary.SelectedValue),
                               new SqlParameter("@convfactor",txtconv.Text),
                               new SqlParameter("@qtydecimalplaces",txtdecimal.Text),
                               new SqlParameter("@CName",username),
                              };
                du.executeprocedure("usp_ins_uom", parm2);
                fillgrid();
                lblMsg.Text = "Unit Added...";
                txtuname.Text = "";
                txtsym.Text = "";
                drpprimary.SelectedItem.Text = "";
                txtconv.Text = "";
                txtdecimal.Text = "";
                txtuname.Focus();
            }
        }
        //SqlConnection con = new SqlConnection(@"Data Source= dis-server\sqlexpress;Initial Catalog=accounts;user id=sa;password=goal123");
        //SqlCommand cmd = new SqlCommand("insert into ivr_uom (uomname,uomsymbol,convfactor,qtydecimalplaces,unitusagetype)values('" + txtuname.Text + "','" + txtsym.Text + "','" + txtconv.Text + "','" + txtdecimal.Text + "','0')",con);
        //try
        //{
        //    con.Open();
        //    cmd.ExecuteNonQuery();
        //    lblMsg.Text = "Data Updated...";
        //}
        //catch (Exception ee)
        //{
        //    lblMsg.Text = ee.Message;
        //}
    } 
    protected void grdDisplay_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Redirect("~/Main/Ledger/Ledger.aspx");
    }
    protected void custPager_PageChanged(object sender, CustomPageChangeArgs e)
    {
        griduom.PageSize = e.CurrentPageSize;
        griduom.PageIndex = e.CurrentPageNumber;
        getdata();
    }

    protected void griduom_RowEditing(object sender, GridViewEditEventArgs e)
    {
        griduom.EditIndex = e.NewEditIndex;
        getdata();
    }
    protected void griduom_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        griduom.EditIndex = -1;
        getdata();
    }
    protected void griduom_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string id = griduom.DataKeys[e.RowIndex].Value.ToString();
        TextBox txtunitname = (TextBox)griduom.Rows[e.RowIndex].FindControl("txtunitname");
        TextBox txtsymbol = (TextBox)griduom.Rows[e.RowIndex].FindControl("txtunitsym");
        string sql = "update IVR_UOM set uomname='"+txtunitname.Text+"',uomsymbol='"+txtsymbol.Text+"' where uomid="+id;
        du.executesql(sql);
        griduom.EditIndex = -1;
        getdata();

    }
    protected void griduom_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        griduom.PageIndex = e.NewPageIndex;
        getdata();
    }
    protected void imgxl_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.ContentType = "application/ms-excel";
            Response.AddHeader("content-disposition", "attachment;filename=UOM_Report.xls");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);

            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            HtmlForm hf = new HtmlForm();
            griduom.Parent.Controls.Add(hf);
            hf.Attributes["runat"] = "server";

            griduom.Columns[griduom.Columns.Count - 1].Visible = false;
            griduom.AllowPaging = false;
            griduom.HeaderRow.Style.Add("width", "15%");
            griduom.HeaderRow.Style.Add("font-size", "10px");
            griduom.Style.Add("text-decoration", "none");
            griduom.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
            griduom.Style.Add("font-size", "8px");
            hf.Controls.Add(griduom);
            hf.RenderControl(htw);

            Response.Write(sw.ToString());
            Response.End();
        }
        catch (Exception ex)
        {

        }
    }
    protected void imgword_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.ContentType = "application/vnd.word";
            Response.AddHeader("content-disposition", "attachment;filename=UOM_Report.doc");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);

            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            HtmlForm hf = new HtmlForm();
            griduom.Parent.Controls.Add(hf);
            hf.Attributes["runat"] = "server";

            griduom.Columns[griduom.Columns.Count - 1].Visible = false;
            griduom.AllowPaging = false;
            griduom.HeaderRow.Style.Add("width", "15%");
            griduom.HeaderRow.Style.Add("font-size", "10px");
            griduom.Style.Add("text-decoration", "none");
            griduom.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
            griduom.Style.Add("font-size", "8px");
            hf.Controls.Add(griduom);
            hf.RenderControl(htw);

            Response.Write(sw.ToString());
            Response.End();
        }
        catch (Exception ex)
        {

        }
    }
    protected void imgpdf_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=UOM_Report.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            HtmlForm hf = new HtmlForm();
            griduom.Parent.Controls.Add(hf);
            hf.Attributes["runat"] = "server";

            griduom.Columns[griduom.Columns.Count - 1].Visible = false;
            griduom.AllowPaging = false;
            griduom.HeaderRow.Style.Add("width", "15%");
            griduom.HeaderRow.Style.Add("font-size", "10px");
            griduom.Style.Add("text-decoration", "none");
            griduom.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
            griduom.Style.Add("font-size", "8px");
            //grdview.DataBind();

            hf.Controls.Add(griduom);
            hf.RenderControl(htw);

            //grdview.RenderControl(htw);           
            StringReader sr = new StringReader(sw.ToString());
            Document pdf = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
            HTMLWorker htmlparser = new HTMLWorker(pdf);
            PdfWriter.GetInstance(pdf, Response.OutputStream);
            pdf.Open();
            htmlparser.Parse(sr);
            pdf.Close();
            Response.Write(pdf);
            Response.End();
        }
        catch (Exception ex)
        {
        }
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        base.VerifyRenderingInServerForm(control);
    }
}
